package com.ai.cmdc.codeutil.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 用户中心myBaits配置
 * @author renjun
 *
 */
@Configuration
@MapperScan(basePackages = {"com.ai.cmdc.user.dao"}, sqlSessionFactoryRef = "sqlSessionFactoryBase")
public class MybatisBaseConfiguration {


	@Autowired
	private DataSource tbucDataSource;


	/**
	 *
	 *〈一句话功能简述〉.
	 *〈功能详细描述〉
	 * @return sqlsessionfactory
	 * @throws Exception 异常
	 */
	@Bean
	public SqlSessionFactory sqlSessionFactoryBase() throws Exception {
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(tbucDataSource);
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		factoryBean.setMapperLocations(
				resolver.getResources("classpath*:sql/oracle/user/*Mapper.xml"));
		return factoryBean.getObject();

	}

	/**
	 *
	 *〈一句话功能简述〉.
	 *〈功能详细描述〉
	 * @return transactionManager
	 */
	@Bean(name = "tbucTransactionManager")
	public DataSourceTransactionManager testTransactionManager() {
		return new DataSourceTransactionManager(tbucDataSource);
	}

	/**
	 *
	 *〈一句话功能简述〉.
	 *〈功能详细描述〉
	 * @return sqlSessionTemplate
	 * @throws Exception 异常
	 */
	@Bean
	public SqlSessionTemplate sqlSessionTemplate() throws Exception {
		return new SqlSessionTemplate(sqlSessionFactoryBase());
	}
}
